import os, sys
import time

class FitsHeader:
    def __init__(self, idx):
        self.header = dict()
        self.header['MY_INDEX']     = idx
        self.header['FILENAME']     = ''
        self.header['CAMERA']       = 'Blue-CAM{}'.format(idx)
        self.header['CHIPTYPE']     = 'E2V290'
        self.header['OBSTYPE']      = ''
        self.header['DATE']         = ''
        self.header['DATE_OBS']     = ''
        self.header['EXPSTART']     = ''
        self.header['EXPTIME']      = ''
        self.header['DARKTIME']     = ''
        self.header['IMGINDEX']     = ''
        self.header['SEQNUM']       = ''
        self.header['WAVELEN']      = ''
        self.header['DATETAG']      = ''
        self.header['SYSTEMP']      = -100

    def get_headers(self):
        return self.header

    def print_header(self):
        # keys = list(self.header.keys())
        # for i in range(len(keys)):
        #     print('{} : {}'.format(keys[i], self.header[keys[i]]))
        
        print('msg_date: {}'.format(time.asctime()))
        for key in self.header:
            print('{} : {}'.format(key, self.header[key]))

    def update_header(self, msg):
        """
        filename:test.fits$obstype:crosstalk$date:19000102$date-obs:19000102$expstart:123.000$exptime:456.000$darktime:789.000$imgindex:1$seqnum:2$wavelen:345.000$datetag:19000102131313$chiptype:e2vccd290$chipnum:a1a2$camera:za3a4$temp:-100.000$glasstype:abc$
        """
#        print('MSG: {}'.format(msg))
        key_vals = msg.split('$')
        for i in range(len(key_vals)):
            if key_vals[i].find(':') != -1:

                if len(key_vals[i].split(':')) < 2:
                    continue
                
                idx = key_vals[i].find(':')
                
                key, val = key_vals[i][0:idx], key_vals[i][idx+1:]
                KEY_UPPER = key.upper()
                
#                print('key_ {}, val_ {}'.format(key,val))

                if KEY_UPPER == 'FILENAME':
                    self.header['FILENAME'] = val
                elif KEY_UPPER == 'CAMERA':
                    self.header['CAMERA']   = val.upper()
                elif KEY_UPPER == 'CHIPTYPE':
                    self.header['CHIPTYPE'] = val.upper()
                elif KEY_UPPER == 'OBSTYPE':
                    self.header['OBSTYPE']  = val
                elif KEY_UPPER == 'DATE':
                    self.header['DATE']     = val
                elif KEY_UPPER == 'DATE-OBS':
                    self.header['DATE_OBS'] = val
                elif KEY_UPPER == 'EXPSTART':
                    self.header['EXPSTART'] = float(val)
                elif KEY_UPPER == 'EXPTIME':
                    self.header['EXPTIME']  = float(val)
                elif KEY_UPPER == 'DARKTIME':
                    self.header['DARKTIME'] = float(val)
                elif KEY_UPPER == 'IMGINDEX':
                    self.header['IMGINDEX'] = int(val)
                elif KEY_UPPER == 'SEQNUM':
                    self.header['SEQNUM']   = int(val)
                elif KEY_UPPER == 'WAVELEN':
                    self.header['WAVELEN']  = float(val)
                elif KEY_UPPER == 'DATETAG':
                    self.header['DATETAG']  = val
                elif KEY_UPPER == 'SYSTEMP':
                    self.header['SYSTEMP']  = float(val)
#                else:
#                    print('unknown header info: {}:{}'.format(key,val))
#            else:
#                print('failed to unpack : {}'.format(key_vals[i]))
                
                self.header['BUNIT'] = 'adu'

def run_test():
    fh = FitsHeader(5)
    fh.print_header()

if __name__ == '__main__':
    run_test()
